﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.MyLayouts;
using QLNH.QLNH_BUS_WS;
using System.Threading;
using QLNH.MyDialogs;

namespace QLNH.MyForms
{
    public partial class AdminTableManagement : FormUC
    {

        #region Properties

        private TableDLG tableDLG = null;

        #endregion

        #region Constructor

        public AdminTableManagement(LayoutUC mainLayout)
        {
            InitializeComponent();
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_TABLE_MANAGEMENT;
        }

        #endregion

        #region Functions

        public override void Resize(Size size)
        {
            if (this.Size.Width < size.Width)
            {
                this.Location = new Point(size.Width / 2 - this.Size.Width / 2, size.Height / 2 - this.Size.Height / 2);
            }
            else
            {
                this.Location = new Point(0, 0);
            }
        }

        //lay danh sach ban
        private void GetAllTable()
        {
            this.lv_tables.Items.Clear();
            Thread t = new Thread(new ThreadStart(GetAllTableThread));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }

        private void GetAllTableThread()
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            TableDTO[] tables = ws.GetAllTable();

            for (int i = 0; i < tables.Length; i++)
            {
                ListViewItem li = new ListViewItem();
                li.Text = tables[i].ID;
                li.Name = tables[i].ID;
                li.SubItems.Add(tables[i].Capacity.ToString());

                if (this.lv_tables.Items.Count % 2 != 0)
                    li.BackColor = Color.LightGray;

                this.lv_tables.BeginInvoke((MethodInvoker)(() => this.lv_tables.Items.Add(li)));
                Thread.Sleep(50);
            }
        }

        //them ban
        private void AddTable(int capacity)
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            if (ws.AddTable(capacity) == true)
            {
                MessageBox.Show("Thêm bàn thành công");
                GetAllTable();
            }
            else
            {
                MessageBox.Show("Thêm bàn thất bại");
            }
        }

        //chinh sua ban
        private void EditTable(TableDTO tableDTO)
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            if (ws.EditTable(tableDTO) == true)
            {
                MessageBox.Show("Cập nhật bàn thành công");
                GetAllTable();
            }
            else
            {
                MessageBox.Show("Cập nhật bàn thất bại");
            }
        }

        //xoa ban
        private void RemoveTable(string id)
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            if (ws.RemoveTable(id) == true)
            {
                MessageBox.Show("Xóa bàn thành công");
                GetAllTable();
            }
            else
            {
                MessageBox.Show("Xóa bàn thất bại");
            }
        }

        #endregion

        #region Control Events

        private ListViewItemComparer sorter;
        private bool ascending = true;

        private void lv_tables_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            sorter = new ListViewItemComparer(e.Column);
            sorter.OrderOfSort = (ascending == true) ? SortOrder.Ascending : SortOrder.Descending;
            if (e.Column == 1)
                sorter.Numeric = true;
            this.lv_tables.ListViewItemSorter = sorter;
            this.lv_tables.Sort();
            ascending = !ascending;
        }

        private void lv_tables_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && this.lv_tables.SelectedItems.Count > 0)
            {
                this.ctm_rightClick.Show(this.lv_tables, e.Location);
            }
        }

        private void AdminTableManagement_Load(object sender, EventArgs e)
        {
            GetAllTable();
            this.tooltip.Show("Nhắp chuột vào đây để sắp xếp lại", this.lv_tables, new Point(0, 0), 1000);
        }

        private void ctm_action(object sender, EventArgs e)
        {
            int tag = Int32.Parse(((ToolStripItem)sender).Tag.ToString());
            switch (tag)
            {
                case 0:
                    if (this.tableDLG == null || this.tableDLG.IsDisposed)
                        this.tableDLG = new TableDLG();
                    this.tableDLG.Edit = true;
                    this.tableDLG.tb_tableCap.Text = this.lv_tables.SelectedItems[0].SubItems[1].Text;
                    string tableID = this.lv_tables.SelectedItems[0].Name;

                    if (this.tableDLG.ShowDialog() == DialogResult.OK)
                    {
                        int capacity = Int32.Parse(this.tableDLG.tb_tableCap.Text.Trim());
                        TableDTO tableDTO = new TableDTO();
                        tableDTO.ID = tableID;
                        tableDTO.Capacity = capacity;
                        EditTable(tableDTO);
                    }
                    break;
                case 1:
                    if (MessageBox.Show("Bạn có chắc chắn muốn xóa bàn này?", "Nhắc nhở", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        string id = this.lv_tables.SelectedItems[0].Name;
                        RemoveTable(id);
                    }
                    break;
                default:
                    break;
            }
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            if (this.tableDLG == null || this.tableDLG.IsDisposed)
                this.tableDLG = new TableDLG();
            this.tableDLG.Edit = false;

            if (this.tableDLG.ShowDialog() == DialogResult.OK)
            {
                int capacity = Int32.Parse(this.tableDLG.tb_tableCap.Text.Trim());
                AddTable(capacity);
            }
        }

        #endregion
    }
}
